package com.first.nell.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.R
import com.first.nell.data.RevObjWalletListFcfa
import com.first.nell.databinding.DialogFirstNellDeleteWalletMobileFcfaBinding
import com.first.nell.extend.addFirstNellUnderFirstNellLine
import com.first.nell.extend.recordFirstNellClickFirstNellEvent

class FcfaDeleteWalletDialog : DialogFragment() {

    companion object {
        fun showFirstNellDialog(
            fragmentManager: FragmentManager,
            itemList: List<RevObjWalletListFcfa>,
            isFcfa: Boolean = true,
            listener: (String) -> Unit
        ) {
            val dialog = FcfaDeleteWalletDialog()
            dialog.mIsFcfa = isFcfa
            dialog.setFirstNellDataFirstNellList(itemList, listener)
            dialog.showNow(fragmentManager, FcfaDeleteWalletDialog::class.java.simpleName)
        }
    }

    lateinit var mFirstNellViewFirstNellBinding: DialogFirstNellDeleteWalletMobileFcfaBinding
    private lateinit var mFcfaRecheckWalletAdapter: FcfaRecheckWalletAdapter

    private var mIsFcfa: Boolean = true

    var mFirstNellOnFirstNellDialogFirstNellListFirstNellItemFirstNellClickFirstNellListener: (String) -> Unit =
        { listItem: String -> }
    val mFirstNellDataFirstNellList = mutableListOf<RevObjWalletListFcfa>()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        mFirstNellViewFirstNellBinding = DialogFirstNellDeleteWalletMobileFcfaBinding.inflate(layoutInflater)
        return mFirstNellViewFirstNellBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initFirstNellSubFirstNellViews()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        mFcfaRecheckWalletAdapter.notifyDataSetChanged()

        mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileCancel.addFirstNellUnderFirstNellLine()

        if(!mIsFcfa){
            mFirstNellViewFirstNellBinding.textDialogDeleteWalletMobileTitle.text = "Are you sure you want to delete this mobile electronic wallet?"
            mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileCancel.text = "Cancel"
            mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileConfirm.mFirstNellViewFirstNellBinding.textSubmitButton.text = "Delete"
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun initFirstNellSubFirstNellViews() {
        mFcfaRecheckWalletAdapter =
            FcfaRecheckWalletAdapter(mFirstNellDataFirstNellList)

        mFirstNellViewFirstNellBinding.rcDialogDeleteWalletMobileList.layoutManager =
            LinearLayoutManager(context)
        mFirstNellViewFirstNellBinding.rcDialogDeleteWalletMobileList.adapter =
            mFcfaRecheckWalletAdapter

        mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileConfirm.setOnClickListener {
            try {
                var mobiles = ""
                mFirstNellDataFirstNellList.forEach { item ->
                    if (mobiles.isBlank()) {
                        mobiles = item.walletMobile
                    } else {
                        mobiles = mobiles + "#" + item.walletMobile
                    }
                }
                mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileConfirm.recordFirstNellClickFirstNellEvent(
                    mobiles
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }

            var ids = ""
            mFirstNellDataFirstNellList.forEach { item ->
                if (ids.isBlank()) {
                    ids = item.id
                } else {
                    ids = ids + "," + item.id
                }
            }
            if (ids.isNotBlank()) {
                mFirstNellOnFirstNellDialogFirstNellListFirstNellItemFirstNellClickFirstNellListener.invoke(ids)
            }
            dismiss()
        }

        mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileCancel.setOnClickListener {
            try {
                var mobiles = ""
                mFirstNellDataFirstNellList.forEach { item ->
                    if (mobiles.isBlank()) {
                        mobiles = item.walletMobile
                    } else {
                        mobiles = mobiles + "#" + item.walletMobile
                    }
                }
                mFirstNellViewFirstNellBinding.submitDialogDeleteWalletMobileCancel.recordFirstNellClickFirstNellEvent(
                    remark = mobiles
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
            dismiss()
        }

        mFirstNellViewFirstNellBinding.imageDialogWalletDeleteClose.setOnClickListener { dismiss() }
    }

    fun setFirstNellDataFirstNellList(
        itemList: List<RevObjWalletListFcfa>,
        listener: (String) -> Unit,
    ) {
        mFirstNellOnFirstNellDialogFirstNellListFirstNellItemFirstNellClickFirstNellListener = listener
        mFirstNellDataFirstNellList.clear()
        mFirstNellDataFirstNellList.addAll(itemList)
    }

}